package aaa0proxy;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ccc {

	public static void main(String[] args) {
		

	}
	
	 public void run() {
		    String line;
		    String host;
		    int port=80;
		        Socket outbound=null;
		    try {
		        socket.setSoTimeout(TIMEOUT);
		        InputStream is=socket.getInputStream();
		        OutputStream os=null;
		        try {
		        // 获取请求行的内容
		        line="";
		        host="";
		        int state=0;
		        boolean space;
		        while (true) {
		            int c=is.read();
		            if (c==-1) break;
		            if (logging) writeLog(c,true);
		            space=Character.isWhitespace((char)c); 
		            switch (state) {
		            case 0:
		            if (space) continue; 
		                state=1;
		            case 1:
		            if (space) {
		                state=2;
		                continue;
		            }
		            line=line+(char)c;
		            break;
		            case 2:
		            if (space) continue; // 跳过多个空白字符
		                  state=3;
		            case 3:
		            if (space) {
		                state=4;
		               // 只分析主机名称部分
		                String host0=host;
		                int n;
		                n=host.indexOf("//");
		                if (n!=-1) host=host.substring(n+2);
		                n=host.indexOf('/');
		                if (n!=-1) host=host.substring(0,n);
		                // 分析可能存在的端口号
		                n=host.indexOf(":");
		                if (n!=-1) { 
		                port=Integer.parseInt(host.substring(n+1));
		                host=host.substring(0,n);
		                }
		                host=processHostName(host0,host,port,socket);
		                if (parent!=null) {
		                host=parent;
		                port=parentPort;
		                }
		                int retry=CONNECT_RETRIES;
		                while (retry--!=0) {
		                try {
		                    outbound=new Socket(host,port);
		                    break;
		                } catch (Exception e) { }
		                // 等待
		                Thread.sleep(CONNECT_PAUSE);
		                }
		                if (outbound==null) break;
		                outbound.setSoTimeout(TIMEOUT);
		                os=outbound.getOutputStream();
		                os.write(line.getBytes());
		                os.write(' ');
		                os.write(host0.getBytes());
		                os.write(' ');
		                pipe(is,outbound.getInputStream(),os,socket.getOutputStream());
		                break;
		            }
		            host=host+(char)c;
		            break;
		            }
		        }
		        }
		        catch (IOException e) { }
		    } catch (Exception e) { }
		    finally {
		            try { socket.close();} catch (Exception e1) {}
		            try { outbound.close();} catch (Exception e2) {}
		       }
		    }

}
